<script src="{{ getAssets }}js/amazeui.datatables.min.js?v={{ assets_version }}"></script>
<script src="{{ getAssets }}js/jquery.table2excel.min.js?v={{ assets_version }}"></script>
<script src="{{ getAssets }}lodop/lodopFuncs.js?v={{ assets_version }}"></script>
<script src="{{ getAssets }}lodop/allocation.js?v={{ assets_version }}"></script>
<script>
    var allocation_id = '{{.m.Id}}';
    var dataTables1 = $('#dataTables').DataTable({
        order: []
        , columns: [
            {
                "bSortable": false, render: function (data, type, full, meta) {
                    return productGroupName(full.Products);
                }
            }
            , {
                "bSortable": false, render: function (data, type, full, meta) {
                    return full.Snd.sph;
                }
            }
            , {
                "bSortable": false, render: function (data, type, full, meta) {
                    return full.Snd.cyl;
                }
            }
            , {
                "bSortable": false, render: function (data, type, full, meta) {
                    return full.Snd.add;
                }
            }
            , {
                "bSortable": false, render: function (data, type, full, meta) {
                    return full.Snd.model;
                }
            }
            , {
                "bSortable": false, render: function (data, type, full, meta) {
                    return full.Snd.color;
                }
            }
            , {
                "bSortable": false, render: function (data, type, full, meta) {
                    return full.Acceptance.lot_number;
                }
            }
            , {
                "bSortable": false, render: function (data, type, full, meta) {
                    return full.Acceptance.manufacture_date > 0 ? timetransDate(full.Acceptance.manufacture_date) : '';
                }
            }
            , {
                "bSortable": false, render: function (data, type, full, meta) {
                    return full.Acceptance.expiry_date > 0 ? timetransDate(full.Acceptance.expiry_date) : '';
                }
            }
            , {
                "data": "purchase_num", "bSortable": false, render: function (data, type, full, meta) {
                    return full.purchase_num
                }
            }
            , {
                "data": "purchase_num", "bSortable": false, render: function (data, type, full, meta) {
                    return full.allocation_num
                }
            }
        ]
        , "serverSide": false
        , "bPaginate": false
        , "bFilter": false	//是否启动过滤、搜索功能
        , "bInfo": false //是否显示页脚信息，DataTables插件左下角显示记录数
        , "bProcessing": true //DataTables载入数据时，是否显示‘进度’提示
        , "drawCallback": function (settings) {
        }
    });
    var isLoading = false;
    var datalist = null;
    $.post('', function (res) {
        var dt = $('#dataTables').DataTable();
        datalist = res.data;
        $.each(datalist, function (index, value) {
            var goods = [];
            value.Inventory.Products = value.Products;
            value.Inventory.Snd = value.Snd;
            value.Inventory.allocation_num = value.AllocationNum;
            dt.row.add(value.Inventory).draw();

        });
        isLoading = true;
    }, 'json');

    $('#download').bind("click", function () {
        if (isLoading) {
            $("#dataTables").table2excel({
                exclude: ".noExl",
                name: "导出调拨明细",
                filename: "调拨明细",
                fileext: ".xls",
                exclude_img: false,
                exclude_links: false,
                exclude_inputs: false
            });
        } else {
            layer.msg("还没加载结束请耐心等待一下！");
            return false;
        }
        return false;
    });

    //退回
    $('#returnBtn').click(function () {
        layer.confirm('确认要退回该单据吗？', function (index) {
            layer.close(index);
            $.post('{{ urlfor "AllocationController.ReturnAllocation" }}', {'allocation_id': allocation_id}, function (res) {
                if (res.code == 1) {
                    layer.alert(res.msg);
                    return false;
                }
                location = '{{ urlfor "AllocationController.Index" }}';
            }, 'json');
        });
    });
    //确认调拨
    $('#confirmBtn').click(function () {
        layer.confirm('确认要调拨该单据吗？', function (index) {
            layer.close(index);
            $.post('{{ urlfor "AllocationController.ConfirmAllocation" }}', {'allocation_id': allocation_id}, function (res) {
                if (res.code == 1) {
                    layer.alert(res.msg);
                    return false;
                }
                layer.alert('调拨完成', function () {
                    location.reload();
                });
            }, 'json');
        });
    });

    $('#printer').click(function () {
        var _data = {};
        _data['number'] = $('[data-type="number"]').text();
        _data['created_at'] = $('[data-type="created_at"]').text();
        _data['formdepname'] = $('[data-type="formdepname"]').text();
        _data['todepname'] = $('[data-type="todepname"]').text();
        _data['operator_name'] = $('[data-type="operator_name"]').text();
        _data['product_number'] = $('[data-type="product_number"]').text();
        _data['remarks'] = $('[data-type="remarks"]').text();
        _data['datalist'] = datalist;
        createAllocationPrinter(_data);
        return false;
    });
</script>